package com.wedimob.boxinternetprotection.freebox;

import java.util.Observable;
import java.util.Observer;


public class FreeboxClient extends Observable implements Observer {
    private Freebox freebox;
    private String mac;
    private String tag;
    private final FreeboxClientStatus status;

    public FreeboxClient(String mac, String tag) {
        super();
        if (mac == null || mac.length() != 17 || mac.split(":").length != 6)
            throw new RuntimeException(mac + " is not a valid mac address");
        this.mac = mac;
        this.tag = tag;
        status = new FreeboxClientStatus(this);
        status.addObserver(this);
    }

    public FreeboxClient(Freebox freebox, String mac, String tag, int inactiveTime, boolean isAssociated, int signal,
            int sentDataSize, int recieveDataSize) {
        super();
        if (mac == null || mac.length() != 17 || mac.split(":").length != 6)
            throw new RuntimeException(mac + " is not a valid mac address");
        this.freebox = freebox;
        this.mac = mac;
        this.tag = tag;
        status = new FreeboxClientStatus(this);
        status.setAssociated(isAssociated);
        status.setInactiveTime(inactiveTime);
        status.setRecieveDataSize(recieveDataSize);
        status.setSentDataSize(sentDataSize);
        status.setSignal(signal);
        status.addObserver(this);
    }

    public String getMac() {
        return mac;
    }

    public String getTag() {
        return tag;
    }

    public Freebox getFreebox() {
        return freebox;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public FreeboxClientStatus getStatus() {
        return status;
    }

    private void checkFreeboxNotNull() {
        if (freebox == null)
            throw new RuntimeException("freebox is null");
    }

    public void addToWhiteList() {
        checkFreeboxNotNull();
        freebox.addtoWhiteList(this);
    }

    public void removeFromWhiteList() {
        checkFreeboxNotNull();
        freebox.removeFromWhiteList(this);
    }

    public void addToBlackList() {
        checkFreeboxNotNull();
        freebox.addtoBlackList(this);
    }

    public void removeFromBlackList() {
        checkFreeboxNotNull();
        freebox.removeFromBlackList(this);
    }

    @Override
    public String toString() {
        return mac;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null)
            return false;

        if (!(o instanceof FreeboxClient))
            return false;

        return mac.equals(((FreeboxClient) o).getMac());
    }

    @Override
    public int hashCode() {
        return mac.hashCode();
    }

    @Override
    public void update(Observable o, Object arg) {
        notifyObservers(arg);
    }
}
